﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace ChibiTD.Common
{
    public class Animation
    {
        //the frames of our animation
        private Rectangle[] mFrames;

        //how long each frame lasts (in seconds)
        private float mFrameLength = 1f / 5f;

        //a timer to use for tracking frame time
        private float mTimer = 0f;

        //the current frame
        int mCurrentFrame = 0;

        /// <summary>
        /// Gets the current frame as a source Rectangle
        /// </summary>
        public Rectangle CurrentFrame
        {
            get { return mFrames[mCurrentFrame]; }
        }

        /// <summary>
        /// Gets or sets the animation speed
        /// </summary>
        public int FramesPerSecond
        {
            //we need to convert from our floating point frameLength
            //variable to an integer number that represents frames per
            //second and vice versa.
            get { return (int)(1f / mFrameLength); }
            set { mFrameLength = 1f / (float)value; }
        }

        /// <summary>
        /// Creates a new Animation
        /// </summary>
        /// <param name="width">The total width of the animation</param>
        /// <param name="height">The height of the animation</param>
        /// <param name="numFrames">The number of frames in the animation</param>
        /// <param name="xOffset">The offset along the X axis</param>
        /// <param name="yOffset">The offset along the Y axis</param>
        public Animation(int width, int height, int numFrames, int xOffset, int yOffset)
        {
            //create our frames array
            mFrames = new Rectangle[numFrames];

            //determine the width of a single frame
            int frameWidth = width / numFrames;

            //create the frame Rectangles
            for (int i = 0; i < numFrames; i++)
            {
                //each Rectangle is created in a row. we use the xOffset as the starting point for
                //the Rectangle's X parameter and add to it for each frame. Since each frame is in
                //a horizontal line, the yOffset is used for the Y parameter. Each frame is the same
                //width and height so we use frameWidth and height for the Rectangle's width and height
                //parameters, respectively.
                mFrames[i] = new Rectangle(xOffset + (frameWidth * i), yOffset, frameWidth, height);
            }
        }

        /// <summary>
        /// Updates the animation
        /// </summary>
        /// <param name="gameTime">The current gameTime</param>
        public void Update(GameTime gameTime)
        {
            //add to our timer based on the time past
            mTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;

            //if the timer has past the frameLength...
            if (mTimer >= mFrameLength)
            {
                //reset the timer to 0
                mTimer = 0f;

                //increment the currentFrame. we use the mod function (%) to make sure
                //our currentFrame stays within the bounds of the frames array.
                mCurrentFrame = (mCurrentFrame + 1) % mFrames.Length;
            }
        }

        /// <summary>
        /// Resets the currentFrame and timer to 0
        /// </summary>
        public void Reset()
        {
            //simply resetting the animation back to 0.
            mCurrentFrame = 0;
            mTimer = 0f;
        }
    }
}
